package com.bsc.rtc.lib.libraries.RecycleViewBase.base;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.ItemTouchHelper;
import androidx.recyclerview.widget.RecyclerView;

import java.util.Collections;
import java.util.List;

/**
 * @auth zzw
 * @Date 2019/8/30 14:21
 * @Desc 参考地址 https://blog.csdn.net/qqqq245425070/article/details/80587271
 */
public class ItemTouchHelperCallBack<T> extends ItemTouchHelper.Callback {
    List<T> mdatas;
    RecyclerView.Adapter myAdapter;
    DeleteCallBack deleteCallBack;

    private float srollDx;

    public ItemTouchHelperCallBack(List<T> mdatas, RecyclerView.Adapter myAdapter, DeleteCallBack deleteCallBack) {
        this.mdatas = mdatas;
        this.myAdapter = myAdapter;
        this.deleteCallBack = deleteCallBack;
    }

    @Override
    public int getMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
       // int swiped = ItemTouchHelper.RIGHT | ItemTouchHelper.LEFT;

         int swiped = 0;
        int dragFlags = 0;
        if (recyclerView.getLayoutManager() instanceof GridLayoutManager) {
            dragFlags = ItemTouchHelper.UP | ItemTouchHelper.DOWN | ItemTouchHelper.RIGHT | ItemTouchHelper.LEFT;
        } else {
            dragFlags = ItemTouchHelper.UP | ItemTouchHelper.DOWN;
        }
        //第一个参数拖动，第二个删除侧滑
        return makeMovementFlags(dragFlags, swiped);
    }

    @Override
    public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
        int oldPosition = viewHolder.getAdapterPosition();
        int newPosition = target.getAdapterPosition();
        if (oldPosition < newPosition) {
            for (int i = oldPosition; i < newPosition; i++) {
                // 改变实际的数据集
                Collections.swap(mdatas, i, i + 1);
            }
        } else {
            for (int i = oldPosition; i > newPosition; i--) {
                // 改变实际的数据集
                Collections.swap(mdatas, i, i - 1);
            }
        }
        myAdapter.notifyItemMoved(oldPosition, newPosition);
        return true;
    }


    @Override
    public void onChildDraw(@NonNull Canvas c, @NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder, float dX, float dY, int actionState, boolean isCurrentlyActive) {
        Log.i("zzw", "onChildDraw=== dx=" + dX + " dy=" + dY);
        //  super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, false);
        if (Math.abs(dX) < 200) {
            super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, false);
        } else if (Math.abs(dX) >= 200) {
            //触发删除事件
            // deleteCallBack.delete(viewHolder.getAdapterPosition());
            Log.i("zzw", "出发滑动事件");
            if (dX > 0) {
                super.onChildDraw(c, recyclerView, viewHolder, 200, dY, actionState, false);
                viewHolder.itemView.scrollTo(0, 0);
            } else {
                isItemViewSwipeEnabled();
                super.onChildDraw(c, recyclerView, viewHolder, -200, dY, actionState, false);
                viewHolder.itemView.scrollTo(200, 0);

            }

        }
    }


    @Override
    public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
        Log.i("zzw", "direction==" + direction);
        int position = viewHolder.getAdapterPosition();
        Log.i("zzw", "scrollX==" + viewHolder.itemView.getScrollX());
        // deleteCallBack.delete(position);
        // viewHolder.itemView.scrollTo(maxSrollDistance, 0);
        //myAdapter.notifyItemChanged(position);
/*        mdatas.remove(position);
        myAdapter.notifyItemRemoved(position);*/
    }

    @Override
    public void onSelectedChanged(RecyclerView.ViewHolder viewHolder, int actionState) {

        if (actionState != ItemTouchHelper.ACTION_STATE_IDLE) {
//            viewHolder.itemView.setBackgroundColor(Color.parseColor("#303F9F"));
        }
    }

    @Override
    public void clearView(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
        super.clearView(recyclerView, viewHolder);
        //  viewHolder.itemView.setBackgroundColor(Color.TRANSPARENT);
    }

    /**
     * 针对swipe状态，swipe滑动的位置超过了百分之多少就消失
     */
    public float getSwipeThreshold(RecyclerView.ViewHolder viewHolder) {
        return 0.8f;
    }

    /**
     * 针对swipe状态，swipe的逃逸速度，换句话说就算没达到getSwipeThreshold设置的距离，达到了这个逃逸速度item也会被swipe消失掉
     */
    public float getSwipeEscapeVelocity(float defaultValue) {
        return 99999f;
    }

    public interface DeleteCallBack {
        boolean delete(int position);
    }
}
